
from django.http import JsonResponse
from django.shortcuts import render
from django.views import View
from rest_framework.permissions import IsAdminUser, IsAuthenticated

from rest_framework.response import Response
from rest_framework.views import APIView

from questions.models import Label, Question, Reply
from questions.serializers import LabelSerializer, QuestionsSerializer
from user.models import User


class QuestionsView(APIView):
    # Path： / labels /
    # Method： GET
    # 接⼝口描述：
    # 获取所有标签列列表
    def get(self, request):
        queryset = Label.objects.all()
        serializer = LabelSerializer(queryset, many=True)
        return Response(serializer.data)


class UsersLabelView(APIView):
    # Path： / labels / users /
    # Method： GET
    # 接⼝口描述：
    # 获取⽤用户关注的标签
    permission_classes = [IsAdminUser]

    def get(self, request):
        user = request.user
        labels = Label.objects.filter(users=user)

        list = []
        for label in labels:
            list.append({
                "id": label.id,
                "label_name": label.name
            })

        return Response(list)


class NewQuestionsView(APIView):
    # Path： / questions / {id} / label / new /
    # Method： GET
    # 接⼝口描述：
    # 获取最新被回答过的问题列列表
    def get(self, request, id):
        if id == "-1":
            questions = Question.objects.filter().order_by("replytime")

        else:
            questions = Question.objects.filter(label_id=id).order_by("replytime")

        serializer = QuestionsSerializer(questions, many=True)
        return Response(serializer.data)


class HotQuestionsView(APIView):
    # Path： / questions / {id} / label / hot /
    # Method： GET
    # 接⼝口描述：
    # 获取回答数较多的问题列列表
    def get(self, request, id):
        if id == "-1":
            questions = Question.objects.filter().order_by("reply")
        else:
            questions = Question.objects.filter(label_id=id).order_by("reply")

        serializer = QuestionsSerializer(questions, many=True)
        return Response(serializer.data)


class WaitQuestionsView(APIView):
    # Path： / questions / {id} / label / wait /
    # Method： GET
    # 接⼝口描述
    def get(self, request, id):
        if id == '-1':
            questions = Question.objects.filter(reply=0)
        else:
            questions = Question.objects.filter(label_id=id, reply=0)

        serializer = QuestionsSerializer(questions, many=True)
        return Response(serializer.data)


class PublishQuestionsView(APIView):
    permission_classes = [IsAuthenticated]

    # Path： / questions /
    # Method： POST
    # 接⼝口描述：
    # 发布⼀一个新的问题
    def post(self, request):
        # user = request.user

        serializer = QuestionsSerializer(data=request.data, context={'request': request})
        if serializer.is_valid():
            serializer.save()

            return Response(serializer.data)
        else:
            return Response({'message': '保存失败'})


class DetQuestionsView(View):
    # Path： / questions / {id} /
    # Method： GET
    # 接⼝口描述：
    # 获取某⼀一个问题的详情信息
    def get(self, request, pk):
        question = Question.objects.get(id=pk)
        answers = Reply.objects.filter(problem_id=pk)
        answer_list = []
        comment_list = []
        for answer in answers:
            answer_list.append(
                {
                    "content": answer.content,
                    "createtime": answer.createtime,
                    "id": answer.id,
                    "parent": answer.parent,
                    "problem": answer.problem,
                    "subs": [],
                    "unuseful_count": answer.unuseful_count,
                    "useful_count": answer.useful_count,
                    "user": {
                        "avatar": answer.user.avatar.url,
                        "id": answer.user.id,
                        "username": answer.user.username
                    }

                }
            )
            comment_list.append(
                {
                    "content": answer.content,
                    "createtime": answer.createtime,
                    "id": answer.id,
                    "parent": answer.parent,
                    "problem": answer.problem,
                    "subs": [],
                    "unuseful_count": answer.unuseful_count,
                    "useful_count": answer.useful_count,
                    "user": {
                        "avatar": answer.user.avatar.url,
                        "id": answer.user.id,
                        "username": answer.user.username
                    }

                }
            )

        labels = question.labels.all()
        label_list = []
        for label in labels:
            label_name = label.label_name
            label_list.append(label_name)

        question_data = {
            "answer_question": answer_list,
            "comment_question": comment_list,
            "content": question.content,
            "createtime": question.createtime,
            "id": question.id,
            "labels": label_list,
            "reply": question.reply,
            "replyname": question.replyname,
            "replytime": question.replytime,
            "title": question.title,
            "unuseful_count": question.unuseful_count,
            "useful_count": question.useful_count,
            "user": question.user.nickname,
            "visits": question.visits,

        }

        return JsonResponse(question_data)
